package com.webflux.controller;

import com.webflux.config.WebSocketWrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

@Slf4j
@RestController
@RequestMapping("/test")
public class Test {
    // 阻塞5秒钟
    private String createStr() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
        }
        return "some string";
    }

    /**
     *  普通的SpringMVC方法
     */
    @GetMapping("/1")
    private String get1() {
        log.info("get1 start");
        String result = createStr();
        log.info("get1 end.");
        return result;
    }

    /**
     * WebFlux(返回的是Mono)
     * @return
     *  测试项目匹配机制，实现webFlux 业务框架
     *  修改项目价值分析图执行定时任务sql 并测试业务，
     *  开发官网项目详情查询接口，添加并替换引导页，
     *  修改项目价值分析图 五维分数设置规则并调试
     *  重新编写项目价值分析图五维分数业务逻辑。
     */
    @GetMapping("/2")
    private Mono<String> get2() {
        log.info("get2 start");
        Mono<String> result = Mono.fromSupplier(() -> createStr());
        log.info("get2 end.");
        return result;
    }

    /**
     * Flux : 返回0-n个元素
     * 注：需要指定MediaType
     * @return
     */
    @GetMapping(value = "/3", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    private Flux<String> flux() {
        Flux<String> result = Flux
                .fromStream(IntStream.range(1, 5).mapToObj(i -> {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                    }
                    log.info("flux data--" + i);
                    return "flux data--" + i;
                }));
        return result;
    }

    @PostMapping("/send")
    public  String sendMessage(@RequestBody Map<String,String> map) {
        String  id = map.get("id");
        String data = map.get("data");
        WebSocketWrap sender = WebSocketWrap.SENDER.get(id);
        if (sender != null) {
            sender.sendText(data);
            return String.format("Message sent to connection:", data, id);
        } else {
            return String.format("Connection of id doesn't exist", id);
        }
    }

    @PostMapping("/sendall")
    public  String sendMessageAll(@RequestBody Map<String,String> map) {
        String data = map.get("data");
        if (data != null) {
            WebSocketWrap.broadcastText(data);
            return String.format("Message sent to connection:", data);
        } else {
            return String.format("Connection of id doesn't exist");
        }
    }

    @PostMapping("/closeSession")
    public  String closeSession(@RequestBody Map<String,String> map) {
        String id = map.get("id");
        if (id != null) {
            WebSocketWrap.closeSession(id);
            return String.format("Close Session id:{}", id);
        } else {
            return String.format("Connection of id doesn't exist");
        }
    }


    public static void main(String[] args) {
        List<Integer> lists =new ArrayList<>();
        lists.add(1);
        lists.add(2);
        lists.add(3);
        lists.add(4);
        lists.add(5);
        for(Integer i : lists){
            System.out.println(i);
            if(lists.size() - 1 != lists.indexOf(i)){
                System.out.println(",");
            }
        }

    }



}
